<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<script type="text/javascript" charset="utf-8">
	var admin_menu_treegrid;
	var editRow;
	var editType;
	var comboTreeData;
	var edit;
	$(function(){
		admin_menu_treegrid=$("#treegrid").treegrid({
			url:'${pageContext.request.contextPath}/menuAction!treeGrid.action',
			idField:'id',
			treeField:'text',
			fit:false,
			fitColumns:true,
			resizable:true,
			columns : [[{
					title:'菜单编号',
					field:'id',
					width:100,
					//checkbox:true
				},{
					title : '菜单名称',
					field : 'text',
					width : 100,
					editor:{
						type:'validatebox',
						options:{
							required:true
						}
					}
				},{
					title : '菜单地址',
					field : 'url',
					width : 100,
					editor:{
						type:'validatebox',
					}
				},{
					title : '菜单图标',
					field : 'iconCls',
					width : 100,
					editor : {
					type : 'combobox',
					options : {
						valueField : 'iconCls',
						textField : 'iconCls',
						panelHeight : '200',
						url:'${pageContext.request.contextPath}/iconClsAction!comboboxInfo.action',
						formatter : function(v) {
							return sy.fs('<span class="{0}" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span>{1}', v.iconCls, v.text);
						}
					}
				}
				},{
					title:'上级名称',
					field:'ptext',
					width:100,
				}]],
				toolbar:[{
					text:'增加',
					iconCls:'icon-add',
					handler:function(){
							append();
					}
				},'-',{
					text:'修改',
					iconCls:'icon-edit',
					handler:function(){
						edit();
					}
					
				},'-',{
					text:'删除',
					iconCls:'icon-remove',
					handler:function(){
						remove();
					}
				},'-',{
					text:'保存',
					iconCls:'icon-save',
					handler:function(){
						admin_menu_treegrid.treegrid('endEdit',editRow.id);
					}
				},'-',{
					text:'取消选中',
					iconCls:'icon-redo',
					handler:function(){
						editRow=undefined;
						admin_menu_treegrid.treegrid('unselectAll');
					}
				},'-',{
					text:'取消编辑',
					iconCls:'icon-undo',
					handler:function(){
						if(editRow){
							admin_menu_treegrid.treegrid('cancelEdit',editRow.id);
							var p=admin_menu_treegrid.treegrid('getParent',editRow.id);
							if(p){
								admin_menu_treegrid.treegrid('reload',p.id);
							}else{
								admin_menu_treegrid.treegrid('reload');
							}
							editRow=undefined
							editType=undefined;
						}
					}
				}],
				onDblClickRow:function(row){
					
					if(editRow!=undefined){
						admin_menu_treegrid.treegrid('endEdit',editRow.id);
					}
					if(editRow==undefined){
						admin_menu_treegrid.treegrid('beginEdit',row.id);
						editRow=row;
						editType='edit';
					}
				},
				onAfterEdit:function(row,changes){
					var url='';
					if(editType=='add'){
						for ( var i = 0; i < row.data.length; i++) {
							if(row.data[i].id==row.ptext){
								console.info(row.ptext);
								row.pid=row.data[i].id;
								break;
							}
						}
						url='${pageContext.request.contextPath}/menuAction!addNode.action';
					}
					if(editType=='edit'){
						url='${pageContext.request.contextPath}/menuAction!update.action';
					}
					$.ajax({
						url:url,
						type:'post',
						data:row,
						dataType:'json',
						success:function(r){
							if(r.success){
								admin_menu_treegrid.treegrid('load');
								$("#layout_west_tree").tree('reload');
								$.messager.show({
									title:'提示',
									msg:r.msg,
								});
								editRow=undefined;
								editType=undefined;
							}else{
								$.messager.show({
									title:'提示',
									msg:r.msg,
								});
							}
						}
						
					});
				}
		});
	});
	function append(){
		if(editRow!=undefined){
			admin_menu_treegrid.treegrid('endEdit',editRow.id);
			editRow=undefined;
		}
		if(editRow==undefined){
			var node=admin_menu_treegrid.treegrid('getSelected');
			if(node!=null){
				var row=[{
					id:sy.UUID(),
					text:'菜单名称',
					ptext:node.text,
					pid:node.id,
				}];
				admin_menu_treegrid.treegrid('append',{
							parent:node.id,
							data:row
						});
				editRow=row[0];
				admin_menu_treegrid.treegrid('select',editRow.id);
				admin_menu_treegrid.treegrid('beginEdit',editRow.id);
			}else{
				againEditorAddRow();
				$.ajax({
					url:'${pageContext.request.contextPath}/menuAction!getAllMenu.action',
					dataType:'json',
					type:'post',
					success:function(data){
						var row=[{
							id:sy.UUID(),
							text:'菜单名称',
							ptext:'上级节点名称',
							pid:'',
							data:data
						}];
						admin_menu_treegrid.treegrid('append',{
							//parent:data.id,
							data:row
						});
						editRow=row[0];
						admin_menu_treegrid.treegrid('select',editRow.id);
						admin_menu_treegrid.treegrid('beginEdit',editRow.id);
					}
				});
			}
			editType='add';
		}
	}
	function remove(){
		var node=admin_menu_treegrid.treegrid('getSelected');
		if(node){
			$.messager.confirm('提示','您确定要删除【'+node.text+'】吗？',function(b){
				if(b){
					$.ajax({
						url:'${pageContext.request.contextPath}/menuAction!delete.action',
						data:{id:node.id},
						type:'post',
						cache:false,
						dataType:'json',
						success:function(j){
							if(j.success){
								$.messager.show({
									title:'提示',
									msg:j.msg
								});
								admin_menu_treegrid.treegrid('load');
								editRow=undefined;
							}else{
								$.messager.alert('提示',j.msg,'warning');
							}
						}
					});
				}
			});
		}else{
			$.messager.alert('提示','请选择要删除的行','warning');
		}
	}
	function edit(){
		var node=admin_menu_treegrid.treegrid('getSelected');
		if(node&&node.id){
			if(editRow!=undefined){
				admin_menu_treegrid.treegrid('endEdit',editRow.id);		
			}
			if(editRow==undefined){
				admin_menu_treegrid.treegrid('beginEdit',node.id);
				editRow=node;
				editType='edit';
			}
		}else{
			$.messager.alert('提示','请选择要修改的行！','warning');
		}
	
	}
	againEditorAddRow=function(){/*无选中添加行时重构Editor*/
		
		admin_menu_treegrid.treegrid('addEditor',{
				field:'ptext',
				editor:{
				type:'combotree',
				options:{
						url:'${pageContext.request.contextPath}/menuAction!getTreeNode.action',
						onLoadSuccess:function(row,data){
							var t=$(this);
							if(data){
							$(data).each(function(index,d){
								if(this.state='closed'){
									t.tree('expandAll');
								};
							});
						};
					},
				}
			}
		});
	};
</script>
<div region="left" border="false">
	<table id="treegrid"></table>
</div>